home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / ddj0492.zip / DFLT11.ZIP / HELPBOX.C < prev    next >
Text File  |  1992-01-09  |  17KB  |  661 lines

  1. /* ------------ helpbox.c ----------- */
  2.  
  3. #include "dflat.h"
  4. #include "htree.h"
  5.  
  6. extern DBOX HelpBox;
  7.  
  8. /* -------- strings of D-Flat classes for calling default
  9.       help text collections -------- */
  10. char *ClassNames[] = {
  11.     #undef ClassDef
  12.     #define ClassDef(c,b,p,a) #c,
  13.     #include "classes.h"
  14.     NULL
  15. };
  16.  
  17. #define MAXHEIGHT (SCREENHEIGHT-10)
  18.  
  19. /* --------- linked list of help text collections -------- */
  20. struct helps {
  21.     char *hname;
  22.     char *NextName;
  23.     char *PrevName;
  24.     long hptr;
  25.     int bit;
  26.     int hheight;
  27.     int hwidth;
  28.     WINDOW hwnd;
  29.     struct helps *NextHelp;
  30. };
  31. static struct helps *FirstHelp;
  32. static struct helps *LastHelp;
  33. static struct helps *ThisHelp;
  34.  
  35. /* --- linked stack of help windows that have beed used --- */
  36. struct HelpStack {
  37.     char *hname;
  38.     struct HelpStack *PrevStack;
  39. };
  40. static struct HelpStack *LastStack;
  41. static struct HelpStack *ThisStack;
  42.  
  43. /* --- linked list of keywords in the current help
  44.            text collection (listhead is in window) -------- */
  45. struct keywords {
  46.     char *hname;
  47.     int lineno;
  48.     int off1, off2, off3;
  49.     int isDefinition;
  50.     struct keywords *nextword;
  51.     struct keywords *prevword;
  52. };
  53.  
  54. static FILE *helpfp;
  55. static char hline [160];
  56. static BOOL Helping;
  57.  
  58. static void SelectHelp(WINDOW, char *);
  59. static void ReadHelp(WINDOW);
  60. static void FindHelp(char *);
  61. static void FindHelpWindow(WINDOW);
  62. static void DisplayDefinition(WINDOW, char *);
  63. static void BestFit(WINDOW, DIALOGWINDOW *);
  64.  
  65. int HelpBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  66. {
  67.     DBOX *db = wnd->extension;
  68.     WINDOW cwnd;
  69.     struct keywords *thisword;
  70.     static char HelpName[50];
  71.  
  72.     switch (msg)    {
  73.         case CREATE_WINDOW:
  74.             Helping = TRUE;
  75.             GetClass(wnd) = HELPBOX;
  76.             InitWindowColors(wnd);
  77.             if (ThisHelp != NULL)
  78.                 ThisHelp->hwnd = wnd;
  79.             break;
  80.         case INITIATE_DIALOG:
  81.             ReadHelp(wnd);
  82.             break;
  83.         case COMMAND:
  84.             if (p2 != 0)
  85.                 break;
  86.             switch ((int)p1)    {
  87.                 case ID_CANCEL:
  88.                     ThisStack = LastStack;
  89.                     while (ThisStack != NULL)    {
  90.                         LastStack = ThisStack->PrevStack;
  91.                         if (ThisStack->hname != NULL)
  92.                             free(ThisStack->hname);
  93.                         free(ThisStack);
  94.                         ThisStack = LastStack;
  95.                     }
  96.                     break;
  97.                 case ID_PREV:
  98.                     FindHelpWindow(wnd);
  99.                     if (ThisHelp != NULL)
  100.                         SelectHelp(wnd, ThisHelp->PrevName);
  101.                     return TRUE;
  102.                 case ID_NEXT:
  103.                     FindHelpWindow(wnd);
  104.                     if (ThisHelp != NULL)
  105.                         SelectHelp(wnd, ThisHelp->NextName);
  106.                     return TRUE;
  107.                 case ID_BACK:
  108.                     if (LastStack != NULL)    {
  109.                         if (LastStack->PrevStack != NULL)    {
  110.                             ThisStack = LastStack->PrevStack;
  111.                             if (LastStack->hname != NULL)
  112.                                 free(LastStack->hname);
  113.                             free(LastStack);
  114.                             LastStack = ThisStack;
  115.                             SelectHelp(wnd, ThisStack->hname);
  116.                         }
  117.                     }
  118.                     return TRUE;
  119.                 default:
  120.                     break;
  121.             }
  122.             break;
  123.         case KEYBOARD:
  124.             if (WindowMoving)
  125.                 break;
  126.             cwnd = ControlWindow(wnd->extension, ID_HELPTEXT);
  127.             if (cwnd == NULL || inFocus != cwnd)
  128.                 break;
  129.             thisword = cwnd->thisword;
  130.             switch ((int)p1)    {
  131.                 case '\r':
  132.                     if (thisword != NULL)    {
  133.                         if (thisword->isDefinition)
  134.                             DisplayDefinition(GetParent(wnd), thisword->hname);
  135.                         else    {
  136.                             strncpy(HelpName, thisword->hname,
  137.                                 sizeof HelpName);
  138.                             SelectHelp(wnd, HelpName);
  139.                         }
  140.                     }
  141.                     return TRUE;
  142.                 case '\t':
  143.                     if (thisword == NULL)
  144.                         thisword = cwnd->firstword;
  145.                     else {
  146.                         if (thisword->nextword == NULL)
  147.                             thisword = cwnd->firstword;
  148.                         else
  149.                             thisword = thisword->nextword;
  150.                     }
  151.                     break;
  152.                 case SHIFT_HT:
  153.                     if (thisword == NULL)
  154.                         thisword = cwnd->lastword;
  155.                     else {
  156.                         if (thisword->prevword == NULL)
  157.                             thisword = cwnd->lastword;
  158.                         else
  159.                             thisword = thisword->prevword;
  160.                     }
  161.                     break;
  162.                 default:
  163.                     thisword = NULL;
  164.                     break;
  165.             }
  166.             if (thisword != NULL)    {
  167.                 cwnd->thisword = thisword;
  168.                 if (thisword->lineno < cwnd->wtop ||
  169.                         thisword->lineno >= cwnd->wtop + ClientHeight(cwnd))    {
  170.                     int distance = ClientHeight(cwnd)/2;
  171.                     do    {
  172.                         cwnd->wtop = thisword->lineno - distance;
  173.                         distance /= 2;
  174.                     }
  175.                     while (cwnd->wtop < 0);
  176.                 }
  177.                 SendMessage(cwnd, PAINT, 0, 0);
  178.                 return TRUE;
  179.             }
  180.             break;
  181.         case CLOSE_WINDOW:
  182.             if (db != NULL)    {
  183.                 if (db->dwnd.title != NULL)    {
  184.                     free(db->dwnd.title);
  185.                     db->dwnd.title = NULL;
  186.                 }
  187.             }
  188.             FindHelpWindow(wnd);
  189.             if (ThisHelp != NULL)
  190.                 ThisHelp->hwnd = NULL;
  191.             Helping = FALSE;
  192.             break;
  193.         default:
  194.             break;
  195.     }
  196.     return BaseWndProc(HELPBOX, wnd, msg, p1, p2);
  197. }
  198.  
  199. static void SelectHelp(WINDOW wnd, char *hname)
  200. {
  201.     if (hname != NULL)    {
  202.         WINDOW pwnd = GetParent(wnd);
  203.         PostMessage(wnd, ENDDIALOG, 0, 0);
  204.         PostMessage(pwnd, DISPLAY_HELP, (PARAM) hname, 0);
  205.     }
  206. }
  207.  
  208. int HelpTextProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  209. {
  210.     struct keywords *thisword;
  211.     int rtn, mx, my;
  212.     switch (msg)    {
  213.         case PAINT:
  214.             if (wnd->thisword != NULL)    {
  215.                 WINDOW pwnd = GetParent(wnd);
  216.                 char *cp;
  217.                 thisword = wnd->thisword;
  218.                 cp = TextLine(wnd, thisword->lineno);
  219.                 cp += thisword->off1;
  220.                 *(cp+1) = (pwnd->WindowColors [SELECT_COLOR] [FG] & 255) | 0x80;
  221.                 *(cp+2) = (pwnd->WindowColors [SELECT_COLOR] [BG] & 255) | 0x80;
  222.                 rtn = DefaultWndProc(wnd, msg, p1, p2);
  223.                 *(cp+1) = (pwnd->WindowColors [HILITE_COLOR] [FG] & 255) | 0x80;
  224.                 *(cp+2) = (pwnd->WindowColors [HILITE_COLOR] [BG] & 255) | 0x80;
  225.                 return rtn;
  226.             }
  227.             break;
  228.         case LEFT_BUTTON:
  229.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  230.             mx = (int)p1 - GetClientLeft(wnd);
  231.             my = (int)p2 - GetClientTop(wnd);
  232.             my += wnd->wtop;
  233.             thisword = wnd->firstword;
  234.             while (thisword != NULL)    {
  235.                 if (my == thisword->lineno)    {
  236.                     if (mx >= thisword->off2 && mx < thisword->off3)    {
  237.                         wnd->thisword = thisword;
  238.                         SendMessage(wnd, PAINT, 0, 0);
  239.                         if (thisword->isDefinition)    {
  240.                             WINDOW pwnd = GetParent(wnd);
  241.                             if (pwnd != NULL)
  242.                                 DisplayDefinition(GetParent(pwnd),
  243.                                     thisword->hname);
  244.                         }
  245.                         break;
  246.                     }
  247.                 }
  248.                 thisword = thisword->nextword;
  249.             }
  250.             return rtn;
  251.         case DOUBLE_CLICK:
  252.             PostMessage(wnd, KEYBOARD, '\r', 0);
  253.             break;
  254.         case CLOSE_WINDOW:
  255.             thisword = wnd->firstword;
  256.             while (thisword != NULL)    {
  257.                 struct keywords *nextword = thisword->nextword;
  258.                 if (thisword->hname != NULL)
  259.                     free(thisword->hname);
  260.                 free(thisword);
  261.                 thisword = nextword;
  262.             }
  263.             break;
  264.         default:
  265.             break;
  266.     }
  267.     return DefaultWndProc(wnd, msg, p1, p2);
  268. }
  269.  
  270. static void *fgetshelp(void)
  271. {
  272.     void *fg;
  273.     do
  274.         if ((fg = GetHelpLine(hline)) == NULL)
  275.             break;
  276.     while (*hline == ';');    /* bypassing all comments */
  277.     return fg;
  278. }
  279.  
  280. static void ReadHelp(WINDOW wnd)
  281. {
  282.     WINDOW cwnd = ControlWindow(wnd->extension, ID_HELPTEXT);
  283.     int linectr = 0;
  284.     if (cwnd == NULL)
  285.         return;
  286.     cwnd->wndproc = HelpTextProc;
  287.     /* ----- read the help text ------- */
  288.     while (TRUE)    {
  289.         unsigned char *cp = hline, *cp1;
  290.         int colorct = 0;
  291.         if (fgetshelp() == NULL)
  292.             break;
  293.         if (*hline == '<')
  294.             break;
  295.         hline[strlen(hline)-1] = '\0';
  296.         /* --- add help text to the help window --- */
  297.         while (cp != NULL)    {
  298.             if ((cp = strchr(cp, '[')) != NULL)    {
  299.                 /* ----- hit a new key word ----- */
  300.                 struct keywords *thisword;
  301.                 if (*(cp+1) != '.' && *(cp+1) != '*')    {
  302.                     cp++;
  303.                     continue;
  304.                 }
  305.                 thisword = calloc(1, sizeof(struct keywords));
  306.                 if (thisword != NULL)    {
  307.                     if (cwnd->firstword == NULL)
  308.                         cwnd->firstword = thisword;
  309.                     if (cwnd->lastword != NULL)    {
  310.                         ((struct keywords *)
  311.                             (cwnd->lastword))->nextword = thisword;
  312.                         thisword->prevword = cwnd->lastword;
  313.                     }
  314.                     cwnd->lastword = thisword;
  315.                     thisword->lineno = cwnd->wlines;
  316.                     thisword->off1 = (int) (cp - hline);
  317.                     thisword->off2 = thisword->off1 - colorct * 4;
  318.                     thisword->isDefinition = *(cp+1) == '*';
  319.                 }
  320.                 colorct++;
  321.                 *cp++ = CHANGECOLOR;
  322.                 *cp++ = (wnd->WindowColors [HILITE_COLOR] [FG] & 255) | 0x80;
  323.                 *cp++ = (wnd->WindowColors [HILITE_COLOR] [BG] & 255) | 0x80;
  324.                 cp1 = cp;
  325.                 if ((cp = strchr(cp, ']')) != NULL)    {
  326.                     if (thisword != NULL)
  327.                         thisword->off3 = thisword->off2 + (int) (cp - cp1);
  328.                     *cp++ = RESETCOLOR;
  329.                 }
  330.                 if ((cp = strchr(cp, '<')) != NULL)    {
  331.                     char *cp1 = strchr(cp, '>');
  332.                     if (cp1 != NULL)    {
  333.                         int len = (int) (cp1 - cp);
  334.                         if ((thisword->hname = calloc(1, len)) != NULL)    
  335.                             strncpy(thisword->hname, cp+1, len-1);
  336.                         memmove(cp, cp1+1, strlen(cp1));
  337.                     }
  338.                 }
  339.             }
  340.         }
  341.         PutItemText(wnd, ID_HELPTEXT, hline);
  342.         /* ---- display help text as soon as window is full ---- */
  343.         if (++linectr == ClientHeight(cwnd))
  344.             SendMessage(cwnd, PAINT, 0, 0);
  345.         if (linectr > ClientHeight(cwnd))    {
  346.             AddAttribute(cwnd, VSCROLLBAR);
  347.             SendMessage(cwnd, BORDER, 0, 0);
  348.         }
  349.     }
  350. }
  351.  
  352. static int HelpLength(char *s)
  353. {
  354.     int len = strlen(s);
  355.     char *cp = strchr(s, '[');
  356.     while (cp != NULL)    {
  357.         len -= 4;
  358.         cp = strchr(cp+1, '[');
  359.     }
  360.     cp = strchr(s, '<');
  361.     while (cp != NULL)    {
  362.         char *cp1 = strchr(cp, '>');
  363.         if (cp1 != NULL)
  364.             len -= (int) (cp1-cp)+1;
  365.         cp = strchr(cp1, '<');
  366.     }
  367.     return len;
  368. }
  369.  
  370. /* ----------- load the help text file ------------ */
  371. void LoadHelpFile()
  372. {
  373.     char *cp;
  374.  
  375.     if (Helping)
  376.         return;
  377.     UnLoadHelpFile();
  378.     if ((helpfp = OpenHelpFile()) == NULL)
  379.         return;
  380.     *hline = '\0';
  381.     while (*hline != '<')    {
  382.         if (fgetshelp() == NULL)    {
  383.             fclose(helpfp);
  384.                return;
  385.         }
  386.     }
  387.     while (*hline == '<')   {
  388.         if (strncmp(hline, "<end>", 5) == 0)
  389.             break;
  390.  
  391.         if ((ThisHelp = calloc(1, sizeof(struct helps))) == NULL)
  392.             break;
  393.  
  394.         if (FirstHelp == NULL)
  395.             FirstHelp = ThisHelp;
  396.  
  397.         /* -------- parse the help window's text name ----- */
  398.         if ((cp = strchr(hline, '>')) == NULL)
  399.             continue;
  400.         *cp = '\0';
  401.         if ((ThisHelp->hname=malloc(strlen(hline+1)+1))==NULL)
  402.             break;
  403.         strcpy(ThisHelp->hname, hline+1);
  404.  
  405.         HelpFilePosition(&ThisHelp->hptr, &ThisHelp->bit);
  406.  
  407.         if (fgetshelp() == NULL)
  408.             break;
  409.  
  410.         /* ------- build the help linked list entry --- */
  411.         while (*hline == '[')    {
  412.             HelpFilePosition(&ThisHelp->hptr, &ThisHelp->bit);
  413.             /* ------ parse the <<prev button pointer ------- */
  414.             if (strncmp(hline, "[<<]", 4) == 0)    {
  415.                 char *cp = strchr(hline+4, '<');
  416.                 if (cp != NULL)    {
  417.                     char *cp1 = strchr(cp, '>');
  418.                     if (cp1 != NULL)    {
  419.                         int len = (int) (cp1-cp);
  420.                         ThisHelp->PrevName = calloc(1, len);
  421.                         if (ThisHelp->PrevName != NULL)
  422.                             strncpy(ThisHelp->PrevName, cp+1, len-1);
  423.                     }
  424.                 }
  425.                 if (fgetshelp() == NULL)
  426.                     break;
  427.                 continue;
  428.             }
  429.             /* ------ parse the next>> button pointer ------- */
  430.             else if (strncmp(hline, "[>>]", 4) == 0)    {
  431.                 char *cp = strchr(hline+4, '<');
  432.                 if (cp != NULL)    {
  433.                     char *cp1 = strchr(cp, '>');
  434.                     if (cp1 != NULL)    {
  435.                         int len = (int) (cp1-cp);
  436.                         ThisHelp->NextName = calloc(1, len);
  437.                         if (ThisHelp->NextName != NULL)
  438.                             strncpy(ThisHelp->NextName, cp+1, len-1);
  439.                     }
  440.                 }
  441.                 if (fgetshelp() == NULL)
  442.                     break;
  443.                 continue;
  444.             }
  445.             else
  446.                 break;
  447.         }
  448.         ThisHelp->hheight = 0;
  449.         ThisHelp->hwidth = 0;
  450.         ThisHelp->NextHelp = NULL;
  451.  
  452.         /* ------ append entry to the linked list ------ */
  453.         if (LastHelp != NULL)
  454.             LastHelp->NextHelp = ThisHelp;
  455.         LastHelp = ThisHelp;
  456.  
  457.         /* -------- move to the next <> token ------ */
  458.         if (fgetshelp() == NULL)
  459.             strcpy(hline, "<end>");
  460.         while (*hline != '<')    {
  461.             ThisHelp->hwidth =
  462.                 max(ThisHelp->hwidth, HelpLength(hline));
  463.             ThisHelp->hheight++;
  464.             if (fgetshelp() == NULL)
  465.                 strcpy(hline, "<end>");
  466.         }
  467.     }
  468.     fclose(helpfp);
  469. }
  470.  
  471. /* ------ free the memory used by the help file table ------ */
  472. void UnLoadHelpFile(void)
  473. {
  474.     while (FirstHelp != NULL)    {
  475.         ThisHelp = FirstHelp;
  476.         if (ThisHelp->hname != NULL)
  477.             free(ThisHelp->hname);
  478.         if (ThisHelp->PrevName != NULL)
  479.             free(ThisHelp->PrevName);
  480.         if (ThisHelp->NextName != NULL)
  481.             free(ThisHelp->NextName);
  482.         FirstHelp = ThisHelp->NextHelp;
  483.         free(ThisHelp);
  484.     }
  485.     ThisHelp = LastHelp = NULL;
  486. }
  487.  
  488. /* ------------ display help text ----------- */
  489. BOOL DisplayHelp(WINDOW wnd, char *Help)
  490. {
  491.     if (Helping)
  492.         return TRUE;
  493.     FindHelp(Help);
  494.     if (ThisHelp != NULL)    {
  495.         if (LastStack == NULL || stricmp(Help, LastStack->hname))    {
  496.             ThisStack = calloc(1,sizeof(struct HelpStack));
  497.             if (ThisStack != NULL)    {
  498.                 ThisStack->hname = malloc(strlen(Help)+1);
  499.                 if (ThisStack->hname != NULL)
  500.                     strcpy(ThisStack->hname, Help);
  501.                 ThisStack->PrevStack = LastStack;
  502.                 LastStack = ThisStack;
  503.             }
  504.         }
  505.         if ((helpfp = OpenHelpFile()) != NULL)    {
  506.             DBOX *db;
  507.             int offset, i;
  508.  
  509.             if ((db = calloc(1,sizeof HelpBox)) != NULL)    {
  510.                 memcpy(db, &HelpBox, sizeof HelpBox);
  511.                 SeekHelpLine(ThisHelp->hptr, ThisHelp->bit);
  512.                    fgetshelp();
  513.                 hline[strlen(hline)-1] = '\0';
  514.                 if ((db->dwnd.title = malloc(strlen(hline)+1)) != NULL)
  515.                     strcpy(db->dwnd.title, hline);
  516.                 db->dwnd.h = min(ThisHelp->hheight, MAXHEIGHT)+7;
  517.                 db->dwnd.w = max(45, ThisHelp->hwidth+6);
  518.                 BestFit(wnd, &db->dwnd);
  519.                 db->ctl[0].dwnd.w = max(40, ThisHelp->hwidth+2);
  520.                 db->ctl[0].dwnd.h = min(ThisHelp->hheight, MAXHEIGHT)+2;
  521.                 offset = (db->dwnd.w-40) / 2;
  522.                 for (i = 1; i < 5; i++)    {
  523.                     db->ctl[i].dwnd.y = min(ThisHelp->hheight, MAXHEIGHT)+3;
  524.                     db->ctl[i].dwnd.x += offset;
  525.                 }
  526.                 if (ThisStack != NULL)
  527.                     if (ThisStack->PrevStack == NULL)
  528.                         DisableButton(db, ID_BACK);
  529.                 if (ThisHelp->NextName == NULL)
  530.                     DisableButton(db, ID_NEXT);
  531.                 if (ThisHelp->PrevName == NULL)
  532.                     DisableButton(db, ID_PREV);
  533.                 DialogBox(wnd, db, TRUE, HelpBoxProc);
  534.                 free(db);
  535.             }
  536.             fclose(helpfp);
  537.             return TRUE;
  538.         }
  539.     }
  540.     return FALSE;
  541. }
  542.  
  543. static void DisplayDefinition(WINDOW wnd, char *def)
  544. {
  545.     WINDOW dwnd;
  546.     WINDOW hwnd = wnd;
  547.     int y;
  548.  
  549.     if (GetClass(wnd) == POPDOWNMENU)
  550.         hwnd = GetParent(wnd);
  551.     y = GetClass(hwnd) == MENUBAR ? 2 : 1;
  552.     FindHelp(def);
  553.     if (ThisHelp != NULL)    {
  554.         clearBIOSbuffer();
  555.         if ((helpfp = OpenHelpFile()) != NULL)    {
  556.             clearBIOSbuffer();
  557.             dwnd = CreateWindow(TEXTBOX,
  558.                                NULL,
  559.                                GetClientLeft(hwnd),
  560.                                GetClientTop(hwnd)+y,
  561.                                min(ThisHelp->hheight, MAXHEIGHT)+3,
  562.                                ThisHelp->hwidth+2,
  563.                                NULL,
  564.                                wnd,
  565.                                NULL,
  566.                                HASBORDER | NOCLIP | SAVESELF);
  567.             if (dwnd != NULL)    {
  568.                 clearBIOSbuffer();
  569.                 /* ----- read the help text ------- */
  570.                 SeekHelpLine(ThisHelp->hptr, ThisHelp->bit);
  571.                 while (TRUE)    {
  572.                     clearBIOSbuffer();
  573.                     if (fgetshelp() == NULL)
  574.                         break;
  575.                     if (*hline == '<')
  576.                         break;
  577.                     hline[strlen(hline)-1] = '\0';
  578.                     SendMessage(dwnd, ADDTEXT, (PARAM) hline, 0);
  579.                 }
  580.                 SendMessage(dwnd, SHOW_WINDOW, 0, 0);
  581.                 SendMessage(NULL, WAITKEYBOARD, 0, 0);
  582.                 SendMessage(NULL, WAITMOUSE, 0, 0);
  583.                 SendMessage(dwnd, CLOSE_WINDOW, 0, 0);
  584.             }
  585.             fclose(helpfp);
  586.         }
  587.     }
  588. }
  589.  
  590. static BOOL wildcmp(char *s1, char *s2)
  591. {
  592.     while (*s1 || *s2)    {
  593.         if (tolower(*s1) != tolower(*s2))
  594.             if (*s1 != '?' && *s2 != '?')
  595.                 return TRUE;
  596.         s1++, s2++;
  597.     }
  598.     return FALSE;
  599. }
  600.  
  601. static void FindHelp(char *Help)
  602. {
  603.     ThisHelp = FirstHelp;
  604.     while (ThisHelp != NULL)    {
  605.         if (wildcmp(Help, ThisHelp->hname) == FALSE)
  606.             break;
  607.         ThisHelp = ThisHelp->NextHelp;
  608.     }
  609. }
  610.  
  611. static void FindHelpWindow(WINDOW wnd)
  612. {
  613.     ThisHelp = FirstHelp;
  614.     while (ThisHelp != NULL)    {
  615.         if (wnd == ThisHelp->hwnd)
  616.             break;
  617.         ThisHelp = ThisHelp->NextHelp;
  618.     }
  619. }
  620.  
  621. static int OverLap(int a, int b)
  622. {
  623.     int ov = a - b;
  624.     if (ov < 0)
  625.         ov = 0;
  626.     return ov;
  627. }
  628.  
  629. /* ------ compute the best location for a dialog box ----- */
  630. static void BestFit(WINDOW wnd, DIALOGWINDOW *dwnd)
  631. {
  632.     int above, below, right, left;
  633.     if (GetClass(wnd) == MENUBAR || GetClass(wnd) == APPLICATION)    {
  634.         dwnd->x = dwnd->y = -1;
  635.         return;
  636.     }
  637.     /* --- compute above overlap ---- */
  638.     above = OverLap(dwnd->h, GetTop(wnd));
  639.     /* --- compute below overlap ---- */
  640.     below = OverLap(GetBottom(wnd), SCREENHEIGHT-dwnd->h);
  641.     /* --- compute right overlap ---- */
  642.     right = OverLap(GetRight(wnd), SCREENWIDTH-dwnd->w);
  643.     /* --- compute left  overlap ---- */
  644.     left = OverLap(dwnd->w, GetLeft(wnd));
  645.  
  646.     if (above < below)
  647.         dwnd->y = max(0, GetTop(wnd)-dwnd->h-2);
  648.     else
  649.         dwnd->y = min(SCREENHEIGHT-dwnd->h, GetBottom(wnd)+2);
  650.     if (right < left)
  651.         dwnd->x = min(GetRight(wnd)+2, SCREENWIDTH-dwnd->w);
  652.     else
  653.         dwnd->x = max(0, GetLeft(wnd)-dwnd->w-2);
  654.  
  655.     if (dwnd->x == GetRight(wnd)+2 || dwnd->x == GetLeft(wnd)-dwnd->w-2)
  656.         dwnd->y = -1;
  657.     if (dwnd->y ==GetTop(wnd)-dwnd->h-2 || dwnd->y == GetBottom(wnd)+2)
  658.         dwnd->x = -1;
  659. }
  660.  
  661.